{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "410984e6-771e-43dc-8ca8-544a8c467e25",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This is main function!\n",
      "['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']\n"
     ]
    }
   ],
   "source": [
    "#! /usr/bin/env python3\n",
    "#-*- encoding: utf-8 -*-\n",
    "import keyword\n",
    "# 查看关键词\n",
    "def python_syntax():\n",
    "    print(keyword.kwlist)\n",
    "if __name__ == '__main__':\n",
    "    print(\"This is main function!\")\n",
    "    python_syntax()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "582be05f-cc18-4b22-9c55-5e0748c14170",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Quant Trade\n",
      "10.68\n",
      "Quant Trade\n",
      "[1, 2, 'a']\n",
      "{'a': 1, 'b': 2}\n"
     ]
    }
   ],
   "source": [
    "print(\"Quant Trade\")\n",
    "print(10.68)\n",
    "\n",
    "str_var = \"Quant Trade\"\n",
    "print(str_var)\n",
    "\n",
    "l_var = [1,2,'a']\n",
    "print(l_var)\n",
    "\n",
    "d_var = {'a':1,'b':2}\n",
    "print(d_var)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4bb4c8a4-7602-4c5f-89a6-faef01f2d0d9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This is Quant Trade and Price is 10.68\n",
      "time is :2019-05-07 21:56:11\n"
     ]
    }
   ],
   "source": [
    "print(\"This is %s and Price is %2.2f\" %(\"Quant Trade\",10.68))\n",
    "str_time = \"2019-05-07 21:56:11\"\n",
    "print(f'time is :{str_time}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "03148ef5-e6bb-4179-a252-2729a504dffc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2019\n",
      "['open', 2019, 2020, [1.1, 1.2]]\n",
      "['close', 'open', 2001, 2020, [1.1, 1.2], '10%']\n",
      "6\n",
      "1\n",
      "['close', 'open', 2020, [1.1, 1.2], '10%', '20%']\n"
     ]
    }
   ],
   "source": [
    "#列表\n",
    "list_temp = ['close','open',2019,2020,[1.1,1.2],\"10%\"]\n",
    "print(list_temp[2])\n",
    "print(list_temp[1:5])\n",
    "list_temp[2] = 2001\n",
    "print(list_temp)\n",
    "print(len(list_temp))\n",
    "print(list_temp.index('open'))\n",
    "list_temp.append(\"20%\")\n",
    "list_temp.remove(2001)\n",
    "print(list_temp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "00b74097-d483-4fdb-b69a-86c188e471ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time is:2019-05-07 21:56:11\n"
     ]
    }
   ],
   "source": [
    "#formatted string literals\n",
    "str_time = \"2019-05-07 21:56:11\"\n",
    "print(f'time is:{str_time}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4000813f-dcc9-4e4c-925e-026e66b1e743",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'bool'>\n",
      "<class 'int'>\n",
      "<class 'float'>\n",
      "<class 'complex'>\n",
      "4\n",
      "-2\n",
      "3\n",
      "0.3333333333333333\n",
      "1\n",
      "1\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "#数值\n",
    "print(type(True))\n",
    "print(type(123))\n",
    "print(type(1.12))\n",
    "print(type(3j + 1))\n",
    "a = 1\n",
    "b = 3\n",
    "print(a + b)\n",
    "print(a - b)\n",
    "print(a * b)\n",
    "print(a / b)\n",
    "print(a  % b)\n",
    "print(a ** b)\n",
    "print(a // b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bd46dec8-c424-4c83-9e42-c1daeaf13edf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "one quote!\n",
      "one ' quote!\n",
      "one \" quote!\n",
      "two ' quote!\n",
      "two \" quote!\n",
      "three quote!\n",
      "three ' quote!\n",
      "three \"\" quote!\n",
      "three \"\"\" quote!\n"
     ]
    }
   ],
   "source": [
    "#字符串\n",
    "print('one quote!')\n",
    "print('one \\' quote!')\n",
    "print('one \" quote!')\n",
    "print(\"two ' quote!\")\n",
    "print(\"two \\\" quote!\")\n",
    "print(\"\"\"three quote!\"\"\")\n",
    "print(\"\"\"three ' quote!\"\"\")\n",
    "print(\"\"\"three \"\" quote!\"\"\")\n",
    "print(\"\"\"three \\\"\"\" quote!\"\"\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6ad0d4cc-df7a-4c1f-b209-06033a907076",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('open', 2019, 2020, [1.1, 1.2])\n",
      "6\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "#元组\n",
    "tuple_temp = ('close','open',2019,2020,[1.1,1.2],\"10%\")\n",
    "print(tuple_temp[1:5])\n",
    "print(len(tuple_temp))\n",
    "print(tuple_temp.index(2019))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7bd7a4c5-21a6-42a3-9c13-879595f5dcbf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'chapter1': 'content1', 'chapter2': 'content1', 'chapter3': 'content1'}\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "unhashable type: 'list'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[9], line 7\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[38;5;28mprint\u001b[39m(dict_temp)\n\u001b[0;32m      6\u001b[0m key \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m2\u001b[39m,\u001b[38;5;241m3\u001b[39m]\n\u001b[1;32m----> 7\u001b[0m dict_temp[key]\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124ma list\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m      8\u001b[0m \u001b[38;5;28mprint\u001b[39m(dict_temp)\n",
      "\u001b[1;31mTypeError\u001b[0m: unhashable type: 'list'"
     ]
    }
   ],
   "source": [
    "#字典\n",
    "dict_temp = {'chapter1':'content1','chapter2':'content1'}\n",
    "key = 'chapter3'\n",
    "dict_temp[key]='content1'\n",
    "print(dict_temp)\n",
    "key = [1,2,3]\n",
    "dict_temp[key]='a list'\n",
    "print(dict_temp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2eeeaa25-da0d-4181-a550-8f9ae9f91c66",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'senior', 'page': 42}\n",
      "senior\n",
      "dict_keys(['chapter1', 'chapter2'])\n",
      "dict_values([{'name': 'basic', 'page': 31}, {'page': 42}])\n",
      "dict_items([('chapter1', {'name': 'basic', 'page': 31}), ('chapter2', {'page': 42})])\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "'chapter3'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[14], line 9\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[38;5;28mprint\u001b[39m(dict_temp\u001b[38;5;241m.\u001b[39mvalues())\n\u001b[0;32m      8\u001b[0m \u001b[38;5;28mprint\u001b[39m(dict_temp\u001b[38;5;241m.\u001b[39mitems())\n\u001b[1;32m----> 9\u001b[0m \u001b[38;5;28mprint\u001b[39m(dict_temp[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mchapter3\u001b[39m\u001b[38;5;124m'\u001b[39m])\n",
      "\u001b[1;31mKeyError\u001b[0m: 'chapter3'"
     ]
    }
   ],
   "source": [
    "#嵌套字典\n",
    "dict_temp = {'chapter1':{'name':\"basic\",'page':31},'chapter2':{'name':\"senior\",'page':42}}\n",
    "print(dict_temp['chapter2'])\n",
    "print(dict_temp['chapter2']['name'])\n",
    "del dict_temp['chapter2']['name']\n",
    "print(dict_temp.keys())\n",
    "print(dict_temp.values())\n",
    "print(dict_temp.items())\n",
    "print(dict_temp['chapter3'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "5df07e0a-5df2-48c9-b62c-eae91aabfbd4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "0x7ffebc942a38\n",
      "6\n",
      "0x7ffebc942a58\n",
      "5\n",
      "0x7ffebc942a38\n",
      "1.5\n",
      "0x234c442efb0\n",
      "1.5\n",
      "0x234c442e4b0\n"
     ]
    }
   ],
   "source": [
    "#数字值一样，内存地址一样\n",
    "i = 5\n",
    "print(i)\n",
    "print(hex(id(i)))\n",
    "i += 1\n",
    "print(i)\n",
    "print(hex(id(i)))\n",
    "j = 5\n",
    "print(j)\n",
    "print(hex(id(j)))\n",
    "i = 1.5\n",
    "print(i)\n",
    "print(hex(id(i)))\n",
    "j = 1.5\n",
    "print(j)\n",
    "print(hex(id(j)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "a27e69f9-2f45-4cdc-b77a-d947445c1947",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3]\n",
      "0x234c44fd100\n",
      "[1, 2, 3, 4]\n",
      "0x234c44fd100\n",
      "[1.5, 2.5, 3.5]\n",
      "0x234c44fc840\n",
      "[1.5, 2.5, 3.5]\n",
      "0x234c44fcfc0\n",
      "[1.5, 2.5, 3.5]\n",
      "0x234c44fcfc0\n",
      "[1.5, 2.5, 3.5]\n",
      "0x234c44fcfc0\n",
      "[1.5, 2.5, 3.5, 4]\n",
      "0x234c44fcfc0\n",
      "[1.5, 2.5, 3.5, 4]\n",
      "0x234c44fcfc0\n"
     ]
    }
   ],
   "source": [
    "i = [1,2,3]\n",
    "print(i)\n",
    "print(hex(id(i)))\n",
    "i.append(4)\n",
    "print(i)\n",
    "print(hex(id(i)))\n",
    "# j、k的值虽然相同，但指向的内存地址却不同\n",
    "j = [1.5,2.5,3.5]\n",
    "print(j)\n",
    "print(hex(id(j)))\n",
    "k = [1.5,2.5,3.5]\n",
    "print(k)\n",
    "print(hex(id(k)))\n",
    "#赋值语句让j、k指向同一个内存地址\n",
    "j = k\n",
    "print(j)\n",
    "print(hex(id(j)))\n",
    "print(k)\n",
    "print(hex(id(k)))\n",
    "#j、k任意一个list变量修改，会影响另外一个list变量的值\n",
    "j.append(4)\n",
    "print(j)\n",
    "print(hex(id(j)))\n",
    "print(k)\n",
    "print(hex(id(k)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "d0ccc67a-758f-43c6-b665-4e7b83eda464",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0x234c4547370 0x234c4547370\n",
      "0x234c451bbb0 0x234c451bbb0\n"
     ]
    }
   ],
   "source": [
    "#字符串内容一样，内存地址一样\n",
    "s1 = 'a' * 20\n",
    "s2 = 'a' * 20\n",
    "print(hex(id(s1)),hex(id(s2)))\n",
    "\n",
    "s1 = 'a' * 21\n",
    "s2 = 'a' * 21\n",
    "print(hex(id(s1)),hex(id(s2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3ce5494c-b5ee-4ac0-aa7b-98cf57b3ce21",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "5\n",
      "4\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "n1921=1921\n",
    "#引用计数\n",
    "print(sys.getrefcount(n1921))\n",
    "getre_val_1 = n1921\n",
    "print(sys.getrefcount(n1921))\n",
    "getre_val_2 = getre_val_1\n",
    "print(sys.getrefcount(n1921))\n",
    "getre_val_3 = [n1921,1922,1923]\n",
    "print(sys.getrefcount(n1921))\n",
    "getre_val_3[0] = 1924\n",
    "print(sys.getrefcount(n1921))\n",
    "del getre_val_1\n",
    "print(sys.getrefcount(n1921))\n",
    "getre_val_2 = 1924\n",
    "print(sys.getrefcount(n1921))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "324358cc-0073-4785-9534-34a7d785ce31",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name= 新希望\n",
      "open= 11.8\n",
      "close= 12\n",
      "args- (14, 16, 17)\n",
      "kwargs= {}\n",
      "args 0-->14\n",
      "args 1-->16\n",
      "args 2-->17\n",
      "name= 新希望\n",
      "open= 11.8\n",
      "close= 14\n",
      "args- (16, 17)\n",
      "kwargs= {'ave': 12, 'high': 15, 'low': 2}\n",
      "args 0-->16\n",
      "args 1-->17\n",
      "kwargs ave-->12\n",
      "kwargs high-->15\n",
      "kwargs low-->2\n",
      "name= 新希望\n",
      "open= 11.8\n",
      "close= 14\n",
      "args- (16, 17)\n",
      "kwargs= {'ave': 12, 'high': 15, 'low': 2}\n",
      "args 0-->16\n",
      "args 1-->17\n",
      "kwargs ave-->12\n",
      "kwargs high-->15\n",
      "kwargs low-->2\n",
      "name= 新希望\n",
      "open= 11.8\n",
      "close= 14\n",
      "args- ((16, 17), {'ave': 12, 'high': 15, 'low': 2})\n",
      "kwargs= {}\n",
      "args 0-->(16, 17)\n",
      "args 1-->{'ave': 12, 'high': 15, 'low': 2}\n"
     ]
    }
   ],
   "source": [
    "# *表示元祖形式传参，**表示字典形式传参\n",
    "# 一般将不重要的参数放在元祖，重要的参数放在字典\n",
    "def stock_info_3(name, open, close=11.5, *args, **kwargs):\n",
    "    print(\"name=\", name)\n",
    "    print(\"open=\", open)\n",
    "    print(\"close=\", close)\n",
    "    print(\"args-\", args)\n",
    "    print(\"kwargs=\", kwargs)\n",
    "\n",
    "    for i, element in enumerate(args):\n",
    "        print(\"args %d-->%s\" % (i, str(element)))\n",
    "\n",
    "    for key in kwargs:\n",
    "        print(\"kwargs %s-->%s\" % (key, kwargs[key]))\n",
    "\n",
    "stock_info_3(u'新希望',11.8,12,14,16,17)\n",
    "stock_info_3(u'新希望',11.8,14,16,17,ave=12,high=15,low=2)\n",
    "aTuple = (16,17)\n",
    "aDict = {'ave':12,'high':15,'low':2}\n",
    "stock_info_3(u'新希望',11.8,14,*aTuple,**aDict)\n",
    "#元祖和字典不带*,当作普通对象\n",
    "stock_info_3(u'新希望',11.8,14,aTuple,aDict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "930ce121-b25a-40db-be65-47e0923d3cf3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "init work\n",
      "Allen-Cart 16\n",
      "<function Human.speak at 0x0000024382BB8900> <function Human.write at 0x0000024382BB82C0> <function Human.walk at 0x0000024382BB8F40>\n",
      "<bound method Human.speak of <__main__.Human object at 0x000002438281AA80>> <bound method Human.write of <__main__.Human object at 0x000002438281AA80>> <bound method Human.walk of <__main__.Human object at 0x000002438281AA80>>\n",
      "Allen-Cart has speak Chinese ability\n",
      "Allen-Cart has write Chinese ability\n",
      "<__main__.Human object at 0x000002438281AA80> has walk ability\n",
      "------------\n",
      "{'__module__': '__main__', 'century': 21, '__init__': <function Human.__init__ at 0x0000024382BB9260>, 'speak': <function Human.speak at 0x0000024382BB8900>, 'write': <function Human.write at 0x0000024382BB82C0>, 'walk': <function Human.walk at 0x0000024382BB8F40>, '__dict__': <attribute '__dict__' of 'Human' objects>, '__weakref__': <attribute '__weakref__' of 'Human' objects>, '__doc__': None}\n",
      "------------\n",
      "{'name': 'Allen-Cart', 'age': 16}\n",
      "22\n",
      "22\n",
      "------------\n",
      "23\n",
      "22\n",
      "{'name': 'Allen-Cart', 'age': 16, 'century': 23}\n",
      "22\n"
     ]
    }
   ],
   "source": [
    "class Human(object):\n",
    "    century = 21\n",
    "    def __init__(self, name, age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        print(\"init work\")\n",
    "\n",
    "    def speak(self, language):\n",
    "        print('%s has speak %s ability'% (self.name, language))\n",
    "\n",
    "    def write(self, word):\n",
    "        print('%s has write %s ability'% (self.name, word))\n",
    "\n",
    "    def walk(self):\n",
    "        print('%s has walk ability'% self)\n",
    "\n",
    "Allen = Human('Allen-Cart', 16)\n",
    "print(Allen.name, Allen.age)\n",
    "print(Human.speak,Human.write,Human.walk)\n",
    "print(Allen.speak,Allen.write,Allen.walk)\n",
    "Allen.speak(\"Chinese\")\n",
    "Allen.write(\"Chinese\")\n",
    "Allen.walk()\n",
    "print(\"------------\")\n",
    "print(Human.__dict__)\n",
    "print(\"------------\")\n",
    "print(Allen.__dict__)\n",
    "#类属性，类引用时，类属性才能更新，实例引用时，不更新，只是创建一个新的实例属性\n",
    "Human.century += 1\n",
    "print(Human.century)\n",
    "print(Allen.century)\n",
    "print(\"------------\")\n",
    "Allen.century += 1\n",
    "print(Allen.century)\n",
    "print(Human.century)\n",
    "print(Allen.__dict__)\n",
    "#删除实例属性\n",
    "del Allen.century\n",
    "#通过实例访问类属性，打印结果：22\n",
    "print(Allen.century)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c965e20b-985a-4adb-9dbd-498383f96db4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "init work\n",
      "{'A': 21}\n",
      "{'A': 21}\n",
      "{'A': 21, 'B': '32'}\n",
      "{'A': 21, 'B': '32'}\n",
      "init work\n",
      "Allen-Cart has speak Chinese ability\n",
      "James has walk ability\n"
     ]
    }
   ],
   "source": [
    "#定义类Human_A Class\n",
    "class Human_A(object):\n",
    "    century = {\"A\":21}\n",
    "\n",
    "    def __init__(self, name, age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        print(\"init work\")\n",
    "\n",
    "    def speak(self,language):\n",
    "        print('%s has speak %s ability'% (self.name, language))\n",
    "\n",
    "    def write(self, word):\n",
    "        print('%s has write %s ability'% (self.name, word))\n",
    "\n",
    "    def walk(self):\n",
    "        print('%s has walk ability'% self)\n",
    "\n",
    "Jahn = Human_A('Jahn-Cart', 22) #打印结果\n",
    "\n",
    "#访问可变变量的类属性\n",
    "print(Human_A.century) #通过类访问\n",
    "print(Jahn.century) #通过实例访问\n",
    "\n",
    "Jahn.century['B'] = \"32\" #通过实例更新\n",
    "\n",
    "print(Human_A.century) #通过类访问，值已被改变\n",
    "print(Jahn.century) #通过实例访问\n",
    "\n",
    "Allen = Human_A('Allen-Cart',16)\n",
    "Allen.speak(\"Chinese\")\n",
    "Human.walk('James')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74fd78ee-e661-4aa7-80b5-3be3f2296840",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Human_B(object):\n",
    "    century = 21\n",
    "    name = \"Allen\"\n",
    "\n",
    "    def __init__(self, name, age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        print(\"init work\")\n",
    "\n",
    "    @staticmethod\n",
    "    def speak(language):\n",
    "        print('%s has speak %s ability'% (Human_B.name,language))\n",
    "\n",
    "    def write(self, word)\n",
    "        print('%s has wirte %s ability'% (self.name, word))\n",
    "\n",
    "    @classmethod\n",
    "    def walk(cls):\n",
    "        print('%s has walk ability'% cls.name)\n",
    "\n",
    "Human_B.speak(\"Chinese\")\n",
    "Human_B.walk()\n",
    "#@staticmethod不需要表示自身对象的self和自身类的cls参数，就和使用函数一样\n",
    "#@classmethod虽然不需要self参数，但第一个参数需要表示自身类的cls参数\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "4024e29b-70f4-4bcd-b9cc-072a6c454411",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(<class '__main__.Human'>,)\n",
      "init work\n",
      "init programmer\n",
      "21 Michael-wang 16 JAVA computer\n",
      "Michael-wang has speak Chinese ability\n",
      "Michael-wang has write Chinese ability\n",
      "Michael-wang has develop ability\n",
      "Michael-wang has speech ability\n",
      "init work\n",
      "init programmer\n",
      "X10 dell\n",
      "X10 has been opened\n",
      "X10 has been closed\n"
     ]
    }
   ],
   "source": [
    "class Human(object):\n",
    "    century = 21\n",
    "    def __init__(self, name, age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        print(\"init work\")\n",
    "\n",
    "    def speak(self, language):\n",
    "        print('%s has speak %s ability'% (self.name, language))\n",
    "\n",
    "    def write(self, word):\n",
    "        print('%s has write %s ability'% (self.name, word))\n",
    "\n",
    "    def walk(self):\n",
    "        print('%s has walk ability'% self)\n",
    "\n",
    "class Programmer(Human):\n",
    "    def __init__(self, name, age, language, tool):\n",
    "        Human.__init__(self, name, age)\n",
    "        self.language = language\n",
    "        self.tool = tool\n",
    "        print(\"init programmer\")\n",
    "\n",
    "    def develop(self):\n",
    "        print('%s has develop ability' % self.name)\n",
    "\n",
    "    def speech(self):\n",
    "        print('%s has speech ability' % self.name)\n",
    "\n",
    "print(Programmer.__bases__)\n",
    "Michael = Programmer('Michael-wang',16,\"JAVA\",\"computer\")\n",
    "print(Michael.century, Michael.name, Michael.age, Michael.language, Michael.tool)\n",
    "Michael.speak(\"Chinese\")\n",
    "Michael.write(\"Chinese\")\n",
    "Michael.develop()\n",
    "Michael.speech()\n",
    "\n",
    "class Computer:\n",
    "    def __init__(self, model, brand):\n",
    "        self.model=model\n",
    "        self.brand=brand\n",
    "\n",
    "    def open_sys(self):\n",
    "        print('%s has been opened' % self.model)\n",
    "\n",
    "    def close_sys(self):\n",
    "        print('%s has been closed' % self.model)\n",
    "        \n",
    "Jack = Programmer('Jack',21,'China',Computer('X10','dell'))\n",
    "print(Jack.tool.model,Jack.tool.brand)\n",
    "Jack.tool.open_sys()\n",
    "Jack.tool.close_sys()\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "3526ec78-8e0f-4fcc-9794-14f623a2f3e8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<list_iterator object at 0x0000024382BBCA90>\n",
      "1\n",
      "2\n",
      "3\n"
     ]
    },
    {
     "ename": "StopIteration",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mStopIteration\u001b[0m                             Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[13], line 8\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28mnext\u001b[39m(its))\n\u001b[0;32m      7\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28mnext\u001b[39m(its))\n\u001b[1;32m----> 8\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28;43mnext\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mits\u001b[49m\u001b[43m)\u001b[49m)\n",
      "\u001b[1;31mStopIteration\u001b[0m: "
     ]
    }
   ],
   "source": [
    "#单步模拟迭代\n",
    "x = [1,2,3]\n",
    "its = x.__iter__()\n",
    "print(its)\n",
    "print(next(its))\n",
    "print(next(its))\n",
    "print(next(its))\n",
    "print(next(its))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "822b4966-2ec8-415c-960a-d7ab9b0dc05e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "#实现可迭代对象\n",
    "class MyRange:\n",
    "    def __init__(self, num):\n",
    "        self.i = 0\n",
    "        self.num = num\n",
    "\n",
    "    def __iter__(self):\n",
    "        return self\n",
    "\n",
    "    def __next__(self):\n",
    "        if self.i < self.num:\n",
    "            i = self.i\n",
    "            self.i += 1\n",
    "            return i\n",
    "        else:\n",
    "            raise StopIteration()\n",
    "\n",
    "for i in MyRange(6):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "795bb597-06c6-4092-b136-4577ebe786f9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 ---> red\n",
      "1 ---> green\n",
      "2 ---> blue\n",
      "3 ---> yellow\n"
     ]
    }
   ],
   "source": [
    "#遍历集合用enumerate写法\n",
    "colors =['red', 'green', 'blue', 'yellow']\n",
    "for i, color in enumerate(colors):\n",
    "    print(i,'--->',color)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f71a2a55-05de-42dc-a4f8-fcf010338fe2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<zip object at 0x000001A5D33BA100>\n",
      "[('raymond', 'red'), ('rachel', 'green'), ('mattew', 'blue')]\n",
      "raymond ---> red\n",
      "rachel ---> green\n",
      "mattew ---> blue\n"
     ]
    }
   ],
   "source": [
    "#遍历多个集合，推荐使用zip\n",
    "names = ['raymond', 'rachel', 'mattew']\n",
    "colors = ['red', 'green', 'blue', 'yellow']\n",
    "\n",
    "print(zip(names, colors)) #返回是一个对象\n",
    "print(list(zip(names, colors))) #list()转换为列表\n",
    "for name, color in zip(names, colors):\n",
    "    print(name, '--->', color)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b0956ad3-c75d-4508-afa2-5e3d552693d3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
